package cn.just.controller;

import cn.just.entity.User;
import cn.just.utils.THUtils;
import org.thymeleaf.context.Context;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * @author wujiaming
 */
@WebServlet(name = "ShowLoginServlet",value="/ShowLoginServlet")
public class ShowLoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /**
         * 获取session中的数据，判断是都有用户信息
         * 有的化说明是登录成功的，直接显示首页
         */
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        if(user !=null){
            response.sendRedirect("HomeServlet");
            return;
        }
        //显示登录页面
        Context context = new Context();

        //获取cookie中的用户名和密码，然后保存到context
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie:cookies){
            //判断保存的是都时用户名
            if(cookie.getName().equals("name")){
                context.setVariable("name",cookie.getValue());
            }
            //判断保存的是都时密码
            if(cookie.getName().equals("pwd")){
                context.setVariable("pwd",cookie.getValue());
            }
        }
        THUtils.write("blog/login",context,response);

    }
}
